﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tomasulo_Simulator
{
    class ReorderBuffer
    {
        int size, head, tail, occupied;
        ReorderBufferEntry[] entries;
        public ReorderBuffer(int size)
        {
            this.size = size;
            this.head = 0;
            this.tail = 0;
            this.occupied = 0;
            entries = new ReorderBufferEntry[size];
        }
        public bool IsFull()
        {
            return occupied == size;
        }
        public bool AddEntry(ReorderBufferEntry entry)
        {
            if (IsFull())
                return false;
            entries[tail] = entry;
            tail++;
            tail %= size;
            occupied++;
            return true;
        }
        public ReorderBufferEntry Entry(int index)
        {
            return entries[index];
        }
        public void Update()
        {
            if (entries[head].Ready)
            {
                entries[head].Commit();
                entries[head] = null;
                head++;
                head %= size;
                occupied--;
            }
        }
        public void Flush()
        {
            head = 0;
            tail = 0;
            occupied = 0;
            for (int i = 0; i < size; i++)
                entries[i] = null;
        }
    }
}
